package com.umpay.privi.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wuwenjie
 * 2013-05-08 肖明修改打印日志的方式
 */
public class PasswordUtil {
	private static Logger logger = LoggerFactory.getLogger(PasswordUtil.class);
	
	public static String genShaPem(String s) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA");
		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(),e);
			return null;
		}
		byte mdData[] = md.digest(s.getBytes());
		String mdPemStr = new String(Base64.encode(mdData));
		return mdPemStr;
	}
	
	/**
	 * 生成随机数
	 * @param length
	 * @return
	 */
	public static String getRandom(int length) {
		 Random r = new Random();
		 String ssource = "0123456789";
		 char[] src = ssource.toCharArray();
		 //产生随机字符串
		 char[] buf = new char[length];
		 int rnd;
		 for (int i=0; i<length; i++) {
		     rnd = Math.abs(r.nextInt()) % src.length;
		     buf[i] = src[rnd];
		 }
		 return new String(buf);
	}
	
	
}
